package com.jstyle.jclifexd.activity;

import android.os.Bundle;
import android.support.v7.widget.SwitchCompat;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;

import com.jstyle.blesdk.cmdenum.SendCmdState;
import com.jstyle.blesdk.constant.DeviceKey;
import com.jstyle.blesdk.model.SportPeriod;
import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.adapter.RecyclerViewBaseAdapter;
import com.jstyle.jclifexd.utils.ScreenUtils;
import com.jstyle.jclifexd.utils.SharedPreferenceUtils;
import com.jstyle.jclifexd.utils.Utils;

import java.util.Map;

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ClockActivity extends BaseActivity implements RecyclerViewBaseAdapter.OnItemClickListener {

    //    @BindView(R.id.RecyclerView_clock)
//    RecyclerView RecyclerViewClock;
    @BindArray(R.array.clock_array)
    String[] clockArray;
    @BindView(R.id.bt_goal_back)
    Button btGoalBack;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_share)
    ImageView ivShare;
    @BindView(R.id.bt_activity_clock)
    Button btActivityClock;
    @BindView(R.id.bt_other_clock)
    Button btOtherClock;
    @BindView(R.id.SwitchCompat_ActivityClock)
    SwitchCompat SwitchCompatActivityClock;
    private SportPeriod sportPeriod;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clock);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        boolean isChecked = SharedPreferenceUtils.getBoolean(SharedPreferenceUtils.KEY_Enable_ActivityClock, true);
        SwitchCompatActivityClock.setChecked(isChecked);
        SwitchCompatActivityClock.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedPreferenceUtils.setSpBoolean(SharedPreferenceUtils.KEY_Enable_ActivityClock, isChecked);
                setActivityClock(isChecked);
                btActivityClock.setEnabled(isChecked);
            }
        });
        btGoalBack.setText(getString(R.string.Alarm_Setting));
        ScreenUtils.setTitleTypeface(btGoalBack);
        ivShare.setVisibility(View.GONE);

    }

    @Override
    public void onItemClick(int position) {
        startActivity(ClockListActivity.class);
    }

    @OnClick({R.id.bt_goal_back, R.id.bt_activity_clock, R.id.bt_other_clock})
    public void onViewClicked(View view) {
        if(Utils.isFastClick()){return;}
        switch (view.getId()) {
            case R.id.bt_goal_back:
                finish();
                break;
            case R.id.bt_activity_clock:
                startActivity(ActivityClockActivity.class);
                break;
            case R.id.bt_other_clock:
                startActivity(ClockListActivity.class);
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        sendData(SendCmdState.READ_SPORT_PERIOD);
    }

    @Override
    public void dataCallback(Map<String, String> maps, SendCmdState sendCmdState) {
        super.dataCallback(maps, sendCmdState);
        switch (sendCmdState) {
            case READ_SPORT_PERIOD:
                int startHour = Integer.valueOf(maps.get(DeviceKey.KSportStartHour));
                int startMin = Integer.valueOf(maps.get(DeviceKey.KSportStartMinter));
                int endHour = Integer.valueOf(maps.get(DeviceKey.KSportEndHour));
                int endMin = Integer.valueOf(maps.get(DeviceKey.KSportEndMinter));
                String timeInterval = maps.get(DeviceKey.KSportNotifierTime);
                String week = maps.get(DeviceKey.KSportWeekEnable);
                String step = maps.get(DeviceKey.KSportMinStep);
                String[] weekStrings = week.split("-");
                int weekInt = 0;
                for (int i = 0; i < 7; i++) {
                    int position = Integer.valueOf(weekStrings[i]);
                    if(position==1)weekInt += Math.pow(2, i);
                }
                sportPeriod = new SportPeriod();
                sportPeriod.setStartHour(startHour);
                sportPeriod.setStartMinute(startMin);
                sportPeriod.setEndHour(endHour);
                sportPeriod.setEndMinute(endMin);
                sportPeriod.setIntervalTime(Integer.valueOf(timeInterval));
                sportPeriod.setLeastStep(Integer.valueOf(step));
                sportPeriod.setWeek(weekInt);
                break;
        }
    }

    private void setActivityClock(boolean enable) {
        if (sportPeriod == null) return;
        sportPeriod.setEnable(enable);
        sendData(SendCmdState.SET_SPORT_PERIOD, sportPeriod);
    }
}
